package lib.gui;

import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import lib.Base.Info;
import lib.Base.Profile;
import lib.Common.Path;
import lib.XML.XMLWriter;

import org.swixml.SwingEngine;

public class GuiProfileEdit {
	/**
	 * Class which represents the profile edit page
	 */
	
	private Info profileInfo;
	private Profile profile;
	private JFrame root;
	private JTextField txt_firstname;
	private JTextField txt_lastname;
	private JTextField txt_birthday_dd;
	private JTextField txt_birthday_mm;
	private JTextField txt_birthday_yyyy;
	private JTextArea txtArea_about;
	
	private GuiProfile parent;
	
	private JButton btn_save;
	private JButton btn_exit;
	
	ActionListener handleSaveClose = new ActionListener(){
		@Override
		public void actionPerformed(ActionEvent e) {
			
			if(e.getActionCommand().equals("save")){
				//Handle Save
				saveChanges();
			}else if(e.getActionCommand().equals("exit")){
				//Handle Close
				closeFrame();
			}
			
		}
	};

	public GuiProfileEdit(Profile profile, Point position) {
		this.profile = profile;

		try {
			new SwingEngine(this).render(new File(Path.GUI_PATH + "gui-profile-edit.xml")).setVisible(true);
			
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		
		initAListener();
		profileInfo = profile.getInfo();
		initFields(profileInfo);
	}
	
	private void initAListener(){
		btn_save.addActionListener(handleSaveClose);
		btn_exit.addActionListener(handleSaveClose);
	}
	
	private void initFields(Info profileInfo){

		txt_firstname.setText(profileInfo.getFirstName());
		txt_lastname.setText(profileInfo.getLastName());
		txt_birthday_dd.setText(String.valueOf(profileInfo.getGebDay()));
		txt_birthday_mm.setText(String.valueOf(profileInfo.getGebMont()));
		txt_birthday_yyyy.setText(String.valueOf(profileInfo.getGebYear()));
		txtArea_about.setText(profileInfo.getAbout());
	}
	
	private void saveChanges(){
		
		int gebDD;
		int gebMM;
		int gebYYYY;
		
		try{
			gebDD = Integer.parseInt(txt_birthday_dd.getText());
			gebMM = Integer.parseInt(txt_birthday_mm.getText());
			gebYYYY =Integer.parseInt(txt_birthday_yyyy.getText());
			
			profileInfo.setFirstName(txt_firstname.getText());
			profileInfo.setLastName(txt_lastname.getText());
			profileInfo.setGebDay(gebDD);
			profileInfo.setGebMonth(gebMM);
			profileInfo.setGebYear(gebYYYY);
			profileInfo.setAbout(txtArea_about.getText());
			
		}catch(NumberFormatException ex){
			JOptionPane.showMessageDialog(null, "Only digits are allowed in the fields [Birthday]", "Birthday Error", JOptionPane.ERROR_MESSAGE);
		}
		profile.setInfo(profileInfo);
		profile.setLastChange(lib.Common.UserId.getTimestamp());
		
		try {
			Thread.sleep(100);
			new XMLWriter().WriteProfile(profile, Path.XML_PATH_PROFILES + profile.getUserId() + ".xml");
		}catch(Exception ew) { System.out.print("gna profile nich geschrieben"); }
	}

	private void closeFrame(){
		root.dispose();
	}
}